<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<title>
		Javascript Audio Processing
	</title>
	<script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js?autoload=true&amp;skin=sunburst&amp;lang=css" defer="defer"></script>
	<!-- show all line numbers-->
	<style type="text/css">
		body {
			font-family:Arial, Helvetica, sans-serif;
		}
		.prettyprint ol.linenums>li {
			list-style-type: decimal
		}
	</style>
	<script src="../build/maximilian.js"></script>
</head>

<body>
	<h1> Maximilian.js Test Zone </h1>
	<p></p>
	<ul>
		<li>Listing input devices in the javascript console</li>
		<li>loading and playing back an .ogg file</li>
	</ul>
	<button id="playButton">Play</button>
	<!-- <pre class="prettyprint lang-js linenums:true" id="quine" style="border:4px solid #88c" > -->
	</pre>
</body>

<!-- Our javascript code -->
<script type="text/javascript">

	let maxi = maximilian();

	function phtest(Larg0,Larg1){
		var tmp0=-0.,tmp1=-0.;
		tmp0=+Larg0.d1;
		Larg0.d4=tmp0;
		if(tmp0>=1){
			tmp1=tmp0+-1;
			Larg0.d1=tmp1;
		}else{
			tmp1=tmp0;
		}
		Larg0.d1=tmp1+1/((+(44100|0))/Larg1);
		return tmp0;
	}



	let playAudio = () => {

		// em++ -s WASM=0
		var maxiEngine = new maxi.maxiAudio();
		var samp = new maxi.maxiSample();
		var imp = new maxi.maxiOsc();
		var ph=new maxi.maxiOsc();

		console.log("Listing input devices: ");
		maxiEngine.listInputDevices();

		maxiEngine.init(0);
		maxiEngine.loadSample("audio/beat2.wav", samp, (sample) => { console.log("Loaded a w " + sample.getLength())} );
		//maxiEngine.loadSample("audio/crebit2.ogg", samp, (sample) => { console.log("Loaded an ogg " + sample.getLength())} );
		// maxiEngine.loadOgg("audio/beat2.wav", samp);
		let count=0;


		maxiEngine.play = function(input) {
			let trig = imp.impulse(3);
			let len = (ph.saw(0.1) + 1) * 0.1 ;
			return samp.playOnZX(trig, 2, 0, len + 0.01);

		}
	}

	const playButton = document.getElementById('playButton');
	playButton.addEventListener("click", () => playAudio());

</script>

</html>
